﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EasyClass {
	public partial class ResplitForm : Form {
		Controller controller;
		public ResplitForm(Controller c) {
			InitializeComponent();
			
			controller = c;
			string[] topics = controller.GetAllTopicsName();
			comboBox1.Items.AddRange(topics);
			listView1.Columns.Add("Topic Name", 100, HorizontalAlignment.Center);
			listView1.Columns.Add("Residual Cap.", -2, HorizontalAlignment.Center);
			foreach (string topicName in topics) {
				ListViewItem lvi = new ListViewItem(topicName);
				lvi.SubItems.Add(controller.GetTopic(topicName).ResidualCapacity().ToString());
				listView1.Items.Add(lvi);
			}
		}

		private void button2_Click(object sender, EventArgs e) {
			Close();
		}

		private void button1_Click(object sender, EventArgs e) {
			if (comboBox1.SelectedIndex < 0) {
				MessageBox.Show("Please select a group");
				return;
			}

			if (listView1.CheckedItems.Count == 0) {
				MessageBox.Show("Please select at least one target topic");
				return;
			}

			List<string> targetNames = new List<string>();
			foreach (ListViewItem lvi in listView1.CheckedItems) {
				targetNames.Add(lvi.Text);
				//MyConsole.WriteLine("target topic: " + lvi.Text);
			}
			
			string topicName = comboBox1.SelectedItem.ToString();
			if (targetNames.Contains(topicName)) {
				MessageBox.Show("Waiting group cannot be one of the selected topics");
				return;
			}
			
			controller.Resplit(topicName, targetNames);
			Close();
		}

		private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
			
		}

		private void ResplitForm_Load(object sender, EventArgs e) {

		}
	}
}
